from django.shortcuts import render
from django.http import HttpResponse
from .models import Question
import json


# Create your views here.
def user_add(request):  # get 传参拿到短信内容 -- 时间自己加上吧还是怎么样  获取当前的时间加上啊
    data = {"index": "val"}
    data_duanxin = request.GET.get('data_duanxin')

    data["data_duanxin"]=data_duanxin
    # data["meth"] = request.POST
    # if data["meth"]:
    #     json_result = json.loads(data["meth"])
    # try:
    #     print("11")
    #     json_result = json.loads(data["meth"])
    #     print("33")
    #     data["post"] = "True"
    #     print("22")
    #     data["json_result_data"] = json_result["data"]
    #     json_result
    #
    #     res = Question.objects.create(question_text=request.post.get("data"))
    #
    # except:
    #     res = Question.objects.create(question_text="username")
    #     print("没有post内容")
    #     data["post"] = "没有post内容"
    # Question.objects.create
    #
    print(f"返回内容{data}")
    json_data = json.dumps(data)
    return HttpResponse(json_data, content_type='application/json')


def user_list(request):
    res = Question.objects.all()[:10]
    # print(res)
    data = {"index": "val"}
    for index_, i in enumerate(res):
        print(i.question_text)
        data[index_] = i.question_text

    json_data = json.dumps(data)

    return HttpResponse(json_data, content_type='application/json')
